/* @(#)PageImpl.java v${project.version} 2011-1-11 上午11:29:09 
 * 
 * ${project.copyright.info}
 * ${project.copyright}
 * ==============================================================================================================*/
package net.lysa.ooo.page;
import java.util.ArrayList;
import java.util.List;
/**
 * 系统分页接口简单实现类.
 * 
 * @see    java.lang.Object
 * @author taoxiangtao
 */
@SuppressWarnings("rawtypes")
public class PageImpl implements Page{
	//~ Constructor
    /**
     * 构造方法，只构造空页
     */
	
	
	public PageImpl() {
        this(0, 0, DEFAULT_PAGE_SIZE, new ArrayList());
    }

    /**
     * 默认构造方法
     *
     * @param start     本页数据在数据库中的起始位置
     * @param totalSize 数据库中总记录条数
     * @param pageSize  本页容量
     * @param data      本页包含的数据
     */
    public PageImpl(int start, int totalSize, int pageSize, List data) {
        this.pageSize = pageSize;
        this.start = start;
        this.totalCount = totalSize;
        this.data = data;
    }

    //~ InterfaceMethod
    /**
     * @see com.guahaoe.sk118.model.page.Page#getTotalCount()
     */
    public int getTotalCount() {
        return this.totalCount;
    }

    /**
     * @see com.guahaoe.sk118.model.page.Page#getTotalCount()
     */
    public int getTotalPageCount() {
        if (totalCount % pageSize == 0)
            return totalCount / pageSize;
        else
            return totalCount / pageSize + 1;
    }

    /**
     * @see com.guahaoe.sk118.model.page.Page#getPageSize()
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * @see com.guahaoe.sk118.model.page.Page#getResult()
     */
    public List getResult() {
        return data;
    }

    /**
     * @see com.guahaoe.sk118.model.page.Page#getCurrentPageNo()
     */
    public int getCurrentPageNo() {
        return (start / pageSize) + 1;
    }

    /**
     * @see com.guahaoe.sk118.model.page.Page#hasNextPage()
     */
    public boolean hasNextPage() {
        return (this.getCurrentPageNo() < this.getTotalPageCount());
    }

    /**
     * @see com.guahaoe.sk118.model.page.Page#hasPreviousPage()
     */
    public boolean hasPreviousPage() {
        return (this.getCurrentPageNo() > 1);
    }
    
    //~ ProtectedMethod
    /**
     * 获取任一页第一条数据的位置，每页条数使用默认值
     */
    protected static int getStartOfPage(int pageNo) {
        return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
    }

    /**
     * 获取任一页第一条数据的位置,startIndex从0开始
     */
    protected static int getStartOfPage(int pageNo, int pageSize) {
        return (pageNo - 1) * pageSize;
    }

    //~ Properties
    private static final long serialVersionUID = 1L;
    private int start;                                //当前页第一条数据的位置,从0开始
    private int pageSize = DEFAULT_PAGE_SIZE; //每页的记录数
    private List data = new ArrayList();              //当前页中存放的记录
    private int totalCount;                           //总记录数
}
